package com.xbn.netty.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

import java.util.concurrent.TimeUnit;

/**
 * @author XBN
 * @date 2020/10/19 13:33
 * @email 78742541@qq.com
 */

public class NettyServerHandler extends SimpleChannelInboundHandler<StudentPOJO.Student> {



    @Override
    protected void channelRead0(ChannelHandlerContext ctx, StudentPOJO.Student msg) throws Exception {

        System.out.println("接收到客户端发送的数据"+"id="+msg.getId()+",name="+msg.getName());

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

        ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端，本条消息是服务器给你的响应"
                ,CharsetUtil.UTF_8)
        );
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
